define(['./../initial', './../service/viewMode', './../service/toggleSideBar', './../service/chartsFactory'], function() {
    return (function() {
        var app = angular.module('App.home');
        app.directive('tabs', ['viewMode', '$rootScope', 'toggleSideBar', 'chartsFactory', function(viewMode, $rootScope, toggleSideBar, chartsFactory) {
            return {
                restrict: 'E',
                transclude: true,
                scope: {},
                controller: ['$scope', '$element',
                    function($scope, $element) {
                        var panes = $scope.panes = [];

                        $scope.select = function(pane) {
                            angular.forEach(panes, function(pane) {
                                pane.selected = false;
                            });
                            pane.selected = true;
                        };

                        $scope.showContext = function() {
                            $('#exportChart').dropdown('show');
                        };
                        $scope.switchValueType = function() {
                            if (!$scope.haveDesignValue()) return;
                            var prj = $rootScope.curPrj;
                            var locationPoints = prj.findLocationPoints(viewMode.monitorType, viewMode.location);
                            locationPoints.isDesignMode = !locationPoints.isDesignMode;
                            toggleSideBar.toggleBottomSideBar('hide').then(function() {
                                //获取一系列的时间的变化量
                                return locationPoints.getRecent2Changes(prj.prjId, viewMode.monitorType, locationPoints.selectedDates, viewMode.location, 0);
                            }).then(function(data) {
                                //一系列的变化曲线
                                return chartsFactory.createPointsChartListByLocation('#chart', data);
                            }).then(function() {
                                return toggleSideBar.toggleBottomSideBar('show');
                            });
                        };
                        $scope.haveDesignValue = function() {
                            if (viewMode.monitorType === '收敛') {
                                return true;
                            } else {
                                return false;
                            }
                        };


                        //导出chart的数据为png或pdf
                        $scope.exportChart = function(exportType) {
                            chartsFactory.exportChart('chart', exportType);
                        };

                        $scope.$on('selectChartPane', function() {
                            var pp = $scope.panes[0];
                            if (pp && pp.selected !== true) {
                                $scope.select(pp);
                            }
                        });
                        this.addPane = function(pane) {
                            if (panes.length === 0) $scope.select(pane);
                            panes.push(pane);
                        };
                    }
                ],
                templateUrl: 'partials/tabsTemplate.html',
                replace: true
            };
        }]);


        app.directive('pane', ['$filter', '$window', '$rootScope',
            function($filter, $window, $rootScope) {
                return {
                    require: '^tabs',
                    restrict: 'E',
                    transclude: true,
                    scope: {
                        header: '@'
                    },
                    link: function(scope, element, attrs, tabsCtrl) {
                        tabsCtrl.addPane(scope);
                        scope.txt = "2000-1-1";
                        if (scope.header === '累积图表') {
                            var drop = $('#exportChart');
                            $('.ui.dropdown').dropdown();
                        }
                    },
                    templateUrl: 'partials/paneTemplate.html',
                    replace: true
                };
            }
        ]);

        return true;
    })();
});
